package com.vip.file.controller;

import com.vip.file.utils.AliyunOSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Api(tags = "OSS模块")
@RequestMapping("/oss/file")
public class OssController {

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;


    @PostMapping("/upload")
    @ApiOperation(value = "上传")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "MultipartFile", dataTypeClass = MultipartFile.class, name = "file",
                    value = "文件", required = true)})
    public void upload(@RequestParam("file") MultipartFile multipartFile) {
        System.out.println(aliyunOSSUtil.upload(multipartFile));
    }

    @PostMapping("/double/upload")
    @ApiOperation(value = "上传")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "MultipartFile", dataTypeClass = MultipartFile.class, name = "file",
                    value = "文件", required = true)})
    public void double_upload(@RequestParam("file") MultipartFile[] multipartFiles) {
        for (MultipartFile multipartFile : multipartFiles) {
            aliyunOSSUtil.upload(multipartFile);
        }
    }

    @GetMapping("/list")
    @ApiOperation(value = "文件列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", dataTypeClass = String.class, name = "filePath",
                    value = "bucket下的文件路径，例如： filePath/", required = true)})
    public void list(@RequestParam("filePath") String filePath) {
        System.out.println(aliyunOSSUtil.getObjectList(filePath));
    }

    @GetMapping("/isExist")
    @ApiOperation(value = "判断文件是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", dataTypeClass = String.class, name = "fileName",
                    value = "例如：filePath/fileName", required = true)})
    public void isExist(@RequestParam("fileName") String fileName) {
        System.out.println(aliyunOSSUtil.isExistFile(fileName));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除文件")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", dataTypeClass = String.class, name = "fileName",
                    value = "例如：filePath/fileName", required = true)})
    public void delete(@RequestParam("fileName") String fileName) {
        System.out.println(aliyunOSSUtil.deleteFile(fileName));
    }

    /**
     * @Description: 服务端签名直传
     **/
    @PostMapping("/uploadToken")
    @ApiOperation(value = "获取uploadToken")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "String", dataTypeClass = String.class, name = "fileName",
                    value = "文件名", required = true)})
    public void uploadToken(@RequestParam("fileName") String fileName) {
        System.out.println(aliyunOSSUtil.getUploadToken(fileName));
    }

}
